import json

from gmssl import sm2, func
import base64


def main():
    private_key = '2C5348E856038A188F60C8F79CEE9CFA871C42994A2E5014BBD6CF9EC17C8195'
    public_key = '206452FCFB2CBE067C3DAC3661E15A5B8557AE481C78110B28F59C5E6ABFA7788C847C587DC68501AE40CEF3C93A6626573570BBD43280C32B83FDF43A4D67A7'
    sm2_crypt = sm2.CryptSM2(
        public_key=public_key, private_key=private_key)
    data = {
        "organization_code": "1000001-0001",
        "count": 5
    }
    data_str = json.dumps(data)
    enc_data = sm2_crypt.encrypt(data_str.encode())
    print("加密后：", enc_data)
    a = base64.b64encode(enc_data)
    print(a)
    b = a.decode()
    print(b)
    c = base64.b64decode(b.encode())
    print(c)
    dec_data = sm2_crypt.decrypt(c)
    d = dec_data.decode()
    print("解密后：", json.loads(d))

    sign = sm2_crypt.sign_with_sm3(data_str.encode())
    print(sign)
    succ = sm2_crypt.verify_with_sm3(sign, data_str.encode())
    print(succ)

    abc = 'payload=n5gKq56sEsVk9OPhUL/owKFN0wrEtgJlGUAWCr48+Dujj4AXRkWE5SliTDjj7RBMpoXmIyzZ/yC4Z6PLFUGfWC3PChxCtJaFOLQj6bFkvrpFrYTFhPz3qRWEojezs9cgsLzB94+oMXqQQyx4rX4lN6XvQu/XLSAWuX8tN2faFAmeAiGAorfyJuStTjEbR3q7HA==&request_id=1238266b63cfa2111edb14c00155d94cf01'
    print(sm2_crypt.sign_with_sm3(abc.encode()))

if __name__ == '__main__':
    main()
